package com.example.phone.Login;


import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.phone.MainActivity;
import com.example.phone.ProductDetails.Detail;
import com.example.phone.R;
import com.example.phone.base.BaseActivity;
import com.example.phone.base.MyApp;
import com.example.phone.base.RequestInterface;
import com.example.phone.base.ServerResponse;
import com.example.phone.bean.User;
import com.example.phone.utils.EditTextUtil;
import com.example.phone.utils.SharedPreferenceUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class LoginActivity extends BaseActivity {
    final String TAG = "!!!!!!!!!!!!!!";
    @BindView(R.id.et_login_username)
    EditText etLoginUsername;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.bt_login_submit)
    Button btLoginSubmit;
    @BindView(R.id.bt_login_register)
    Button btLoginRegister;
    @BindView(R.id.cb_remember_login)
    CheckBox cbRememberLogin;
    @BindView(R.id.tv_login_forget_pwd)
    TextView tvLoginForgetPwd;
    @BindView(R.id.iv_login_username_del)
    ImageView ivLoginUsernameDel;
    @BindView(R.id.iv_login_pwd_del)
    ImageView ivLoginPwdDel;
    EditTextUtil delete = new EditTextUtil();//删除输入框中的内容

    @Override
    public int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        String loginNameStr = (String) SharedPreferenceUtil.get(this, "username", ""/*这儿不能放null，因为辅助类中取值的时候会根据这的值的类型取对应类型的值，之前问题就出在这里*/);
        String loginPwdStr = (String) SharedPreferenceUtil.get(this, "password", "");

        boolean rememberLogin = (boolean) SharedPreferenceUtil.get(this, "rememberLogin", false);


        if (rememberLogin) {
            cbRememberLogin.setChecked(rememberLogin);
            etLoginUsername.setText(loginNameStr);
            etLoginPwd.setText(loginPwdStr);
            doLogin(loginNameStr, loginPwdStr);
        }
    }

    @OnClick({R.id.bt_login_submit, R.id.bt_login_register, R.id.cb_remember_login, R.id.tv_login_forget_pwd,
            R.id.iv_login_username_del, R.id.iv_login_pwd_del})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_login_submit:
                toastShort("登陆");
                beginLogin();

                break;
            case R.id.bt_login_register:
                toastShort("注册");
                goNextActivity(RegisterActivity.class);//转跳到注册界面
                break;
            case R.id.cb_remember_login:
                toastShort("自动登陆");

                break;
            case R.id.tv_login_forget_pwd:
                toastShort("忘记密码");
                break;
            case R.id.iv_login_username_del:
                delete.clearButtonListener(etLoginUsername,ivLoginUsernameDel);
                break;
            case R.id.iv_login_pwd_del:
                delete.clearButtonListener(etLoginPwd,ivLoginPwdDel);
                break;
        }
    }

    //登录的开始
    private void beginLogin() {
        String loginNameString = etLoginUsername.getText().toString().trim();
        String loginPwdString = etLoginPwd.getText().toString().trim();

        if (TextUtils.isEmpty(loginNameString)) {
            toastShort("用户名不能为空！");
            return;
        }
        if (TextUtils.isEmpty(loginPwdString)) {
            toastShort("密码不能为空！");
            return;
        }

        if (cbRememberLogin.isChecked()) {
            //把账号、密码存到sharedPreference
            SharedPreferenceUtil.put(this, "username", loginNameString);
            SharedPreferenceUtil.put(this, "password", loginPwdString);

            SharedPreferenceUtil.put(this, "rememberLogin", true);
        } else {
            SharedPreferenceUtil.put(this, "account", null);
            SharedPreferenceUtil.put(this, "password", null);
            SharedPreferenceUtil.put(this, "rememberLogin", false);
        }
        doLogin(loginNameString, loginPwdString);
    }

    /**
     * 真正的登陆逻辑
     *
     * @param loginNameString 账号
     * @param loginPwdString  密码
     */

    private void doLogin(String loginNameString, String loginPwdString) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.Login3 requestInterfaceLogin = retrofit.create(RequestInterface.Login3.class);
        Call<ServerResponse<User>> call = requestInterfaceLogin.login(loginNameString, loginPwdString);

        call.enqueue(new Callback<ServerResponse<User>>() {

            @Override
            public void onResponse(Call<ServerResponse<User>> call, Response<ServerResponse<User>> response) {

                if (response.body() == null) {
                    toastShort("未获取到相关数据,请检查网络配置！");
                    return;
                }
                if (response.body().getStatus() == 0) {

                    User user = response.body().getData();


                    MyApp myApp = (MyApp) getApplication();//将用户信息存入在Application中
                    myApp.setUser(user);
                    int useId = myApp.getUser().getId();
                    Log.d(TAG, useId + "");

                    toastShort("登陆成功！");
                    goNextActivity(MainActivity.class);
                } else {
                    toastShort("登陆失败");
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<User>> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                toastShort("登陆失败");
            }
        });
    }

}
